using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Example.Business;

namespace www
{
    public partial class Search_ByRegion : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void CompanyDataSource_Select(object sender, NBusiness.Data.Web.SelectEntityEventArgs e)
        {
            int regionId;
            if (int.TryParse(Request["r"], out regionId) && regionId > 0)
            {
                int totalEntities = 0;
                e.Entities = CompanyCollection.FetchByRegion(
                    regionId,
                    e.StartIndex,
                    e.MaxRows,
                    e.SortField,
                    e.SortOrder,
                    out totalEntities);
                e.TotalRows = totalEntities;
            }
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                Example.Business.Company c = (Example.Business.Company)e.Row.DataItem;

                Label l = (Label)e.Row.FindControl("LabelAddress");
                for (int x = 0; x < c.Addresses.Count; x++)
                {
                    if (x > 0) l.Text += "<br>";

                    Address a = c.Addresses[x];
                    l.Text += a.City;
                }
            }
        }
        protected void Label1_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int regionId;
                if (int.TryParse(Request["r"], out regionId) && regionId > 0)
                {
                    Region r = Region.Fetch(regionId);
                    Label1.Text = r.Name + ", " + r.Country.Name;
                }
            }
        }
    }
}